package com.domain.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.oa.entity.PageVo;
import com.domain.oa.mapper.MenuMapper;
import com.domain.oa.entity.Menu;
import com.domain.oa.service.MenuService;
import com.domain.oa.utils.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Menu)表服务实现类
 *
 * @author makejava
 * @since 2023-02-20 15:07:36
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Resource
    private MenuMapper menuMapper;

    @Override
    public ResultVo getDataByPage(Integer page, Integer pageSize) {
        Page<Menu> page1 = new Page<>(page, pageSize);
        QueryWrapper<Menu> query = new QueryWrapper<>();
        Page<Menu> result = page(page1, query);
        return new ResultVo(200, "分页查询菜单成功！", result);
    }

    @Override
    public ResultVo editData(Menu data) {
        //设置操作时间
        Date date = new Date();
        data.setUpdateTime(date);

        try {
            saveOrUpdate(data);
            return new ResultVo(200, "修改菜单成功！",null);
        } catch (Exception e) {
            return new ResultVo(400, "操作菜单失败！", e.getMessage());
        }

    }

    @Override
    public ResultVo deleteDataById(Integer id) {
        try {
            removeById(id);
            return new ResultVo(200, "删除菜单成功！",null);
        } catch (Exception e) {
            return new ResultVo(400, "删除菜单失败！", e.getMessage());
        }
    }

    @Override
    public ResultVo getMenuByPageToTree(Integer pageCurrent, Integer pageSize) {
        try {
            List<Menu> menu = menuMapper.getMenuByPageToTree((pageCurrent-1)*pageSize, pageSize);

            Integer total = menuMapper.getTotal();

            // menu的父子级筛选
            List<Menu> resultMenu = treeList(menu);

            // 实例化分页数据对象存放数据
            PageVo pageVo = new PageVo();

            pageVo.setPage(pageCurrent);
            // 存放每页大小
            pageVo.setPageSize(pageSize);
            // 存放当前页
            pageVo.setCurrent(pageCurrent);
            // 存放总数据数
            pageVo.setTotal(total);
            // 存放总页码
            pageVo.setTotalPageNum((total + pageSize - 1)/pageSize);
            // 存放分页的数据
            pageVo.setResult(resultMenu);

            // 返回统一封装结果
            return new ResultVo(200, "分页查询菜单成功!", pageVo);
        }catch (Exception e) {
            return new ResultVo(400, "获取菜单信息失败!", e.getMessage());
        }




    }

    /**
     * 获取所有菜单权限
     * @return
     */
    @Override
    public ResultVo getAllMenu() {
        try {
            List<Menu> menu = menuMapper.getALlMenu();
            List<Menu> resultMenu = treeList(menu);
            return new ResultVo(200, "获取所有权限成功!", resultMenu);
        }catch (Exception e) {
            return new ResultVo(505, "获取所有权限失败", e.getMessage());
        }

    }

    /**
     * 获取相关角色的权限菜单
     * @param roleId
     * @return
     */
    @Override
    public ResultVo getMenuByRoleId(Integer roleId) {
        try {
            List<Menu> menuByRoleId = menuMapper.getMenuByRoleId(roleId);
            List<Menu> resultMenu = treeList(menuByRoleId);
            return new ResultVo(200, "获取所有权限成功!", resultMenu);
        }catch (Exception e) {
            return new ResultVo(505, "获取所有权限失败", e.getMessage());
        }
    }

    /**
     * 进行二叉树分类方法
     * @param menu
     * @return
     */
    public static List<Menu> treeList(List<Menu> menu) {
        List<Menu> parentMenu = menu.stream().filter(m -> m.getMenuPid().equals(0)).collect(Collectors.toList());

        for (Menu pMenu : parentMenu) {
            List<Menu> result = menu.stream().filter(m -> m.getMenuPid().equals(pMenu.getMenuId())).collect(Collectors.toList());
            pMenu.setChildren(result);
        }

        return parentMenu;
    }


}

